<?php
namespace app\admin\controller;
use app\admin\logic\MenuLogic;
use think\Request;
use think\Db;
Class Menu extends BaseAdmin {

    public function index()
    {
        return $this->fetch();
    }

    public function ajax()
    {
        $title = $this->_get('title','');
        $limit = $this->_get('limit',10);
        $res = (new MenuLogic())->listing($title,$limit);
        return $res;
    }

    public function update(Request $request)
    {
        $menu = new MenuLogic();
        if ($request->isPost()) {
            $data = $request->post();

            $menu->update($data);
            return apiReturnSuc();
        } else {
            $id = $this->_get('id');
            $r = $menu->update($id, 'get');
            $this->assign('info', $r);
            return $this->fetch();
        }
    }
    
    // 删除
    public function delete($id)
    {
        $r = (new MenuLogic())->delete($id);
        return apiReturnSuc($r);
    }
    
    // 添加某分类下的二级栏目
    public function createlm(Request $request)
    {
        $MenuLogic = new MenuLogic();
        if($request->isPost()) {
            $data = $request->post();
            $MenuLogic->createlm($data);
            return apiReturnSuc();
        } else {
            $id = $this->_get('id');
            $this->assign('pid',$id);
            return $this->fetch();
        }
    }

    // c
    public function create(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->post();
            $MenuLogic = new MenuLogic();
            $MenuLogic->create($data);
            return apiReturnSuc();
        } else {
            $re = Db::table('menu')->where(['status'=>1])->order('id')->select();
            $this->assign('res',$re);
            return $this->fetch();
        }
    }

}